package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.entity.Feedback;
import com.xxx.entity.dto.FeedbackDto;
import com.xxx.service.IFeedbackService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 客服留言表（反馈表） 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-26
 */
@RestController
@RequestMapping("/feedback")
@Tag(name = "客服留言管理")
public class FeedbackController {

    @Autowired
    private IFeedbackService feedbackService;

    @PostMapping("/add")
    @Operation(summary = "添加客服留言")
    public Result add(@RequestBody Feedback feedback){
        Boolean flag = feedbackService.save(feedback);
        return flag ? Result.success("添加客服留言成功") : Result.error("添加客服留言失败");
    }

    @GetMapping("/delete/{feedbackId}")
    @Operation(summary = "删除客服留言")
    public Result delete(@PathVariable Integer feedbackId){
        Boolean flag = feedbackService.removeById(feedbackId);
        return flag ? Result.success("删除客服留言成功") : Result.error("不存在该客服留言Id");
    }

    @PutMapping("/update")
    @Operation(summary = "修改客服留言")
    public Result update(@RequestBody Feedback feedback){
        Boolean flag = feedbackService.updateById(feedback);
        return flag ? Result.success("修改客服留言成功") : Result.error("修改客服留言失败");
    }

    @GetMapping("/selectByPage/{current}/{size}")
    @Operation(summary = "分页查询客服留言")
    public Result selectByPage(@PathVariable Integer current, @PathVariable Integer size){
        IPage<Feedback> page = feedbackService.page(new Page<>(current,size));
        return Result.success(page);
    }

    @PostMapping("/selectByCondition")
    @Operation(summary = "根据客服留言Id、用户Id、状态条件查询客服留言")
    public Result selectByCondition(@RequestBody FeedbackDto feedbackDto){
        LambdaQueryWrapper<Feedback> lqw = new LambdaQueryWrapper<>();
        lqw.eq(feedbackDto.getFeedbackStatus()!=null,Feedback::getFeedbackStatus,feedbackDto.getFeedbackStatus())
                .eq(feedbackDto.getUserId()!=null,Feedback::getUserId,feedbackDto.getUserId())
                .eq(feedbackDto.getFeedbackId()!=null,Feedback::getFeedbackId,feedbackDto.getFeedbackId());
        IPage<Feedback> page = feedbackService.page(new Page<>(feedbackDto.getCurrent(), feedbackDto.getSize()),
                lqw);
        return Result.success(page);
    }
}

